using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using CRM.Extensions;
using CRM.Models;
using CRM.Models.Email;
using HotChocolate;
using HotChocolate.Authorization;
using HotChocolate.Data;
using HotChocolate.Types;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace CRM.Graphql;

[Authorize]
public partial class Query
{
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailMarketing>> GetEmailMarketings(int? state, string? title, int[]? ids, DateTime[]? createdAt,  ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailMarketing
                .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                            m.UserId == user.Id)
                .WhenWhere(ids.IsNotNull(), m => ids.Contains(m.Id))
                .WhenWhere(!string.IsNullOrEmpty(title),
                    m => m.Title.StartsWith(title) || m.Title.EndsWith(title))
                .WhenWhere(state != 0 && state != null,
                    m => m.State == (EmailMarketing.StateEnum)state)
                .WhenWhere(createdAt != null && createdAt.Length == 2,
                    m => m.CreatedAt >= createdAt[0] && m.CreatedAt <= createdAt[1].AddDays(1));
    }
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailConfigGroup>> GetEmailConfigGroups(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailConfigGroup
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailConfigGroup>> GetEmailConfigGroupList(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailConfigGroup
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailConfig>> GetEmailConfigs(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailConfig
            .Include(e => e.EmailConfigGroup)
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailConfig>> GetEmailConfigsList(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailConfig
            .Include(e => e.EmailConfigGroup)
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailSendHistory>> GetEmailSendHistories(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailSendHistory
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailTemplate>> GetEmailTemplates(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailTemplate
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
    
    [UseSingleOrDefault]
    public async Task<IEnumerable<EmailTemplate>> GetEmailTemplateById(int id, ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailTemplate
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id)
            .Where(m => m.Id == id);
    }
    
    [UseOffsetPaging, UseFiltering, UseSorting]
    public async Task<IEnumerable<EmailBlackList>> GetEmailBlackLists(ClaimsPrincipal claims,
        [Service] UserManager<User> userManager, [Service] MyDbContext dbContext)
    {
        var user = await userManager.GetUserAsync(claims);
        return dbContext.EmailBlackLists
            .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId &&
                        m.UserId == user.Id);
    }
}